package com.gukun.springboot.tdengine.service.writeplant;

import com.taosdata.jdbc.ws.TSWSPreparedStatement;

import java.sql.*;

/**
 * Prepare target database.
 * Count total records in database periodically so that we can estimate the writing speed.
 */
public class DataBaseMonitor {
    private Connection conn;
    private Statement stmt;

    private TSWSPreparedStatement pst;

    public DataBaseMonitor init(String jdbcURL) throws SQLException {
        if (conn == null) {
            if(!jdbcURL.contains("batchfetch=true")){
                conn = DriverManager.getConnection(jdbcURL);
                stmt = conn.createStatement();
            }else{
                conn = DriverManager.getConnection(jdbcURL,"root","taosdata");
                stmt = (Statement)conn.createStatement();
            }

        }
        return this;
    }

    public void close() {
        try {
            stmt.close();
            stmt = null;
        } catch (SQLException e) {
        }
        try {
            conn.close();
            conn = null;
        } catch (SQLException e) {
        }
    }

    public void prepareDatabase(TDConfigDto tdConfigDto,Boolean createDatabase) throws SQLException {
        String dbName = tdConfigDto.getDbName();
        if(createDatabase){
            stmt.execute("DROP DATABASE IF EXISTS "+dbName);
            stmt.execute("CREATE DATABASE "+dbName +" VGROUPS "+tdConfigDto.getVgroups()+" BUFFER "+tdConfigDto.getBuffer()+" ");
        }else{
            //stmt.execute("CREATE DATABASE  IF NOT EXISTS "+dbName +" VGROUPS "+tdConfigDto.getVgroups()+" BUFFER "+tdConfigDto.getBuffer()+" ");
        }
//        stmt.execute("DROP DATABASE IF EXISTS "+dbName);
//        stmt.execute("CREATE DATABASE  IF NOT EXISTS "+dbName +" VGROUPS "+tdConfigDto.getVgroups()+" BUFFER "+tdConfigDto.getBuffer()+" ");
//        stmt.execute("CREATE STABLE  IF NOT EXISTS  "+dbName+".stagfloat (ts TIMESTAMP, v FLOAT, q TINYINT) TAGS (tagid BIGINT)");
//        stmt.execute("CREATE STABLE IF NOT EXISTS  "+dbName+".stagdouble (ts TIMESTAMP, v DOUBLE, q TINYINT) TAGS (tagid BIGINT)");
//        stmt.execute("CREATE STABLE IF NOT EXISTS  "+dbName+".stagint (ts TIMESTAMP, v INT, q TINYINT) TAGS (tagid BIGINT)");
//        stmt.execute("CREATE STABLE IF NOT EXISTS  "+dbName+".stagbool (ts TIMESTAMP, v BOOL, q TINYINT) TAGS (tagid BIGINT)");
        stmt.execute("use "+dbName+";");
        stmt.execute("CREATE STABLE  IF NOT EXISTS  "+dbName+".tfloat (ts TIMESTAMP, v FLOAT) TAGS (ti nchar(1024))");
        stmt.execute("CREATE STABLE IF NOT EXISTS  "+dbName+".tdouble (ts TIMESTAMP, v DOUBLE) TAGS (ti nchar(1024))");
        stmt.execute("CREATE STABLE IF NOT EXISTS  "+dbName+".tint (ts TIMESTAMP, v INT) TAGS (ti nchar(1024))");
        stmt.execute("CREATE STABLE IF NOT EXISTS  "+dbName+".tbool (ts TIMESTAMP, v BOOL) TAGS (ti nchar(1024))");
    }

    public long count(String dbName) throws SQLException {
        try (ResultSet result = stmt.executeQuery("SELECT count(*) from "+dbName+".tfloat")) {
            result.next();
            return result.getLong(1);
        }
    }

    public long getTableCount(String dbName) throws SQLException {
        try (ResultSet result = stmt.executeQuery("select count(*) from information_schema.ins_tables where db_name = '"+dbName+"';")) {
            result.next();
            return result.getLong(1);
        }
    }
}